package com.lrc.dentalbackend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lrc.dentalbackend.common.BaseContext;
import com.lrc.dentalbackend.common.ResponseResult;
import com.lrc.dentalbackend.domain.dto.AdminLoginDTO;
import com.lrc.dentalbackend.domain.dto.ArticleDTO;
import com.lrc.dentalbackend.domain.dto.ArticleQueryDTO;
import com.lrc.dentalbackend.domain.entity.Article;
import com.lrc.dentalbackend.service.AdminService;
import com.lrc.dentalbackend.service.ArticleService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin")
public class AdminController {
    @Resource
    private AdminService adminService;

    @Resource
    private ArticleService articleService;

    @ApiOperation("管理员登录")
    @PostMapping("/login")
    public ResponseResult login(@RequestBody AdminLoginDTO admin){
        return ResponseResult.ok(adminService.login(admin));
    }

    @ApiOperation("登出")
    @PostMapping("/logout")
    public ResponseResult logout(){
        BaseContext.removeCurrentId();
        return ResponseResult.ok();
    }

    @ApiOperation("添加文章")
    @PostMapping("/add/article")
    public ResponseResult addArticle(@RequestBody ArticleDTO articleDTO){
        articleService.addArticle(articleDTO);
        return ResponseResult.ok();
    }

    @ApiOperation("删除文章")
    @PostMapping("/del/article/{id}")
    public ResponseResult delArticle(@PathVariable Long id){
        articleService.delById(id);
        return ResponseResult.ok();
    }

    @ApiOperation("根据id查询文章")
    @GetMapping("/get/article/{id}")
    public ResponseResult getByArticleId(@PathVariable Long id){
        if (id == null){
            throw new RuntimeException("id不能为空");
        }
        Article article = articleService.getById(id);
        return ResponseResult.ok(article);
    }

    @ApiOperation("更新文章内容或标题")
    @PostMapping("/update/article")
    private ResponseResult updateArticle(@RequestBody ArticleDTO article){
        articleService.updateArticleById(article);
        return ResponseResult.ok();
    }

    @ApiOperation("分页并且可通过文章标题或内容模糊查询")
    @PostMapping("/get/articles")
    private ResponseResult getPageListAndSearch(@RequestBody ArticleQueryDTO articleQuery){
        Page<Article> page = articleService.getPageListAndSearch(articleQuery);
        return ResponseResult.ok(page);
    }
}
